package com.paypalpay.controller.common;

import com.paypalpay.entity.common.DevicePackageCoverPicture;
import com.paypalpay.entity.common.InternationalPayPackage;
import com.paypalpay.pojo.Constant;
import com.paypalpay.pojo.request.RequestBody.*;
import com.paypalpay.pojo.request.RequestHeader;
import com.paypalpay.pojo.request.subRequest.*;
import com.paypalpay.pojo.response.subResponse.*;
import com.paypalpay.pojo.response.subResponse.common.RefuelingPackage;
import com.paypalpay.pojo.response.subResponse.common.SaleInfo;
import com.paypalpay.pojo.response.subResponse.common._Package;
import com.paypalpay.service.InternationalPayPackageService;
import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
import com.paypalpay.util.JaxbUtil;
import com.paypalpay.util.SignUtil;
import com.paypalpay.util.TimeUtil;
import com.paypalpay.util.wx.CommonResponse;
import com.paypalpay.util.wx.HttpClientTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//localhost:8081/paypalpay/module/syncModulePackages
@RestController
@RequestMapping("/module")
/**
 * 国际支付半闭环项目：
 * 设备控制类
 *
 * @author lm
 */
public class ModuleController {

    @Autowired
    private HttpSession session;

    @Autowired
    public InternationalPayPackageService internationalPayPackageService;

    /**
     * 获取设备套餐列表
     *
     * @param body 请求体  凝趣 测试渠道编码 P000512
     * @return
     */
    @RequestMapping(value = "syncModulePackages", method = RequestMethod.GET)
    @ResponseBody
    public String syncModulePackages(SyncModulePackagesRequestBody body, @RequestParam(value = "partnerCode") String partnerCode) {
        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curtime = df.format(new Date());
        // 请求参数拼接
        SyncModulePackagesRequest request = new SyncModulePackagesRequest();
        TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
        RequestHeader header = utils.getRequestHeader(Constant.cmd_syncModulePackages, partnerCode);
        request.setHeader(header);
        request.setBody(body);
        String sign = getSign(header, body, body.getClass());
        request.setSign(sign);

        // 将参数转换成XML格式
        String sRequest = new JaxbUtil(SyncModulePackagesRequest.class).toXml(request, "utf-8");
        System.out.println("请求值：" + sRequest);
        // 发送post请求，并接收返回结果
        String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
        System.out.println("返回结果：" + result);
        // 将回传的xml转换成object数据
        SyncModulePackagesResponse responseObj = new JaxbUtil(SyncModulePackagesResponse.class).fromXml(result, "utf-8");
        //<access_code>P000512</access_code>
        //把渠道编码取出来好入库
        //String partnerCode = responseObj.getHeader().getAccessCode();
        // 将SyncModulePackagesResponse收到的package遍历出来
        List<_Package> packageList = responseObj.getBody().get_package();
        // 普通套餐
        for (_Package p : packageList) {
            String packageName = p.getPackageName();
            String packageCode = p.getPackageCode();
            System.out.println("获取到的产品编码：" + packageCode);
            String packageType = p.getPackageType();
            String buyType = p.getBuyType();
            String orderUnit = p.getOrderUnit();
            String flow = p.getFlow();
            String orderPeriod = p.getOrderPeriod();
            String coverCountryMcc = p.getCoverCountryMcc();
            //String coverCountrys = p.getCoverCountrys();
            String maxDeviceNum = p.getMaxDeviceNum();
            String mccs = "";
            String continentName = "";
            String coverCountrys = "";
            String coverCountryCode;
            // 用packageCode前两位字母+后6位数字，位作为coverCountryCode值
            coverCountryCode = packageCode.substring(0, 2) + packageCode.substring(packageCode.length() - 6, packageCode.length());
            System.out.println("最终的coverCountryCode是" + coverCountryCode);
            String partnerName = internationalPayPackageService.queryPartnerNameBypartnerCode(partnerCode);
            System.out.println("取到的渠道名称：" + partnerName);
            //将mcc用；截取，然后拿着数据去查询
            String[] strs = coverCountryMcc.split(";");
            for (int i = 0; i < strs.length; i++) {
                mccs = strs[i];
                continentName += internationalPayPackageService.queryContinent(mccs) + ";";
                coverCountrys += internationalPayPackageService.queryCountry(mccs) + ";";
            }
            // 最后一个，去掉
            if (!"".equals(continentName) || !"".equals(coverCountrys)) {
                continentName = continentName.substring(0, continentName.length() - 1);
                coverCountrys = coverCountrys.substring(0, coverCountrys.length() - 1);
            }

            // 加油包
            List<RefuelingPackage> refuelingPackageList = p.getRefuelingPackage();
            if (refuelingPackageList == null || refuelingPackageList.size() < 0) {

                //套餐售卖信息，表明该套餐可以供哪些产品使用，售价是多少。
                List<SaleInfo> saleInfoList = p.getSaleInfo();
                if (saleInfoList != null || saleInfoList.size() > 0) {
                    for (SaleInfo saleInfo : saleInfoList) {
                        String salePrice = saleInfo.getSalePrice();
                        String appScope = saleInfo.getAppScope();

                        int count = internationalPayPackageService.findPackageExistOrNot(packageCode, partnerCode);
                        System.out.println("当前count的数值是：" + count);

                        if (count <= 0) {
                            InternationalPayPackage payPackage = new InternationalPayPackage();
                            payPackage.setPartnerCode(partnerCode);
                            payPackage.setPartnerName(partnerName);
                            payPackage.setPackageName(packageName);
                            payPackage.setPackageCode(packageCode);
                            payPackage.setCoverCountryCode(coverCountryCode);
                            payPackage.setPackageType(packageType);
                            payPackage.setBuyType(buyType);
                            payPackage.setOrderUnit(orderUnit);
                            payPackage.setFlow(flow);
                            payPackage.setOrderPeriod(orderPeriod);
                            payPackage.setCoverCountryMcc(coverCountryMcc);
                            payPackage.setCoverCountrys(coverCountrys);
                            payPackage.setContinentName(continentName);

                            payPackage.setSalePrice(salePrice);
                            payPackage.setAppScope(appScope);
                            session.setAttribute("appScope", appScope);
                            // 如果数据库中不存在当前套餐就入库套餐
                            internationalPayPackageService.bak_storagePackage(payPackage);
                            System.out.println("device_package_international_pay_t流量套餐入库完成！");
                        }
                        int c=internationalPayPackageService.findPictureExistOrNot(packageCode, partnerCode);
                        if (c <= 0) {

                            DevicePackageCoverPicture picture=new DevicePackageCoverPicture();
                            picture.setPartnerCode(partnerCode);
                            picture.setPartnerName(partnerName);
                            picture.setPackageCode(packageCode);
                            picture.setCoverCountryCode(coverCountryCode);
                            picture.setCoverCountrys(coverCountrys);
                            picture.setCreateTime(curtime);
                            internationalPayPackageService.storagePicture(picture);
                            System.out.println("device_package_cover_picture_t入库完成！");
                        }

                    }

                }
            } else {
                for (RefuelingPackage refueling : refuelingPackageList) {
                    //套餐售卖信息，表明该套餐可以供哪些产品使用，售价是多少。
                    List<SaleInfo> saleInfoList = p.getSaleInfo();
                    if (saleInfoList != null || saleInfoList.size() > 0) {
                        for (SaleInfo saleInfo : saleInfoList) {
                            String salePrice = saleInfo.getSalePrice();
                            String appScope = saleInfo.getAppScope();

                            String refPackageName = refueling.getRefPackageName();
                            String refPackageCode = refueling.getRefPackageCode();
                            String refPackageFlow = refueling.getRefPackageFlow();

                            int count = internationalPayPackageService.findPackageExistOrNot(packageCode, partnerCode);
                            System.out.println("当前count的数值是：" + count);
                            if (count <= 0) {
                                InternationalPayPackage payPackage = new InternationalPayPackage();
                                payPackage.setPartnerCode(partnerCode);
                                payPackage.setPartnerName(partnerName);
                                payPackage.setPackageName(packageName);
                                payPackage.setPackageCode(packageCode);
                                payPackage.setCoverCountryCode(coverCountryCode);
                                payPackage.setPackageType(packageType);
                                payPackage.setBuyType(buyType);
                                payPackage.setOrderUnit(orderUnit);
                                payPackage.setFlow(flow);
                                payPackage.setOrderPeriod(orderPeriod);
                                payPackage.setCoverCountryMcc(coverCountryMcc);
                                payPackage.setCoverCountrys(coverCountrys);
                                payPackage.setContinentName(continentName);

                                payPackage.setMaxDeviceNum(maxDeviceNum);

                                payPackage.setSalePrice(salePrice);
                                payPackage.setAppScope(appScope);
                                session.setAttribute("appScope", appScope);

                                payPackage.setRefPackageName(refPackageName);
                                payPackage.setRefPackageCode(refPackageCode);
                                payPackage.setRefPackageFlow(refPackageFlow);
                                // 如果数据库中不存在当前套餐就入库套餐
                                internationalPayPackageService.bak_storageRefuelingPackage(payPackage);
                                System.out.println("device_package_international_pay_t流量池入库完成！");
                            }

                            int c=internationalPayPackageService.findPictureExistOrNot(packageCode, partnerCode);
                            if (c <= 0) {

                                DevicePackageCoverPicture picture=new DevicePackageCoverPicture();
                                picture.setPartnerCode(partnerCode);
                                picture.setPartnerName(partnerName);
                                picture.setPackageCode(packageCode);
                                picture.setCoverCountryCode(coverCountryCode);
                                picture.setCoverCountrys(coverCountrys);
                                picture.setCreateTime(curtime);
                                internationalPayPackageService.storagePicture(picture);
                                System.out.println("device_package_cover_picture_t入库完成！");
                            }

                        }
                    }

                }
            }
        }
        // 返回错误结果的处理
        String retCode = responseObj.getHeader().getResultCode();

        if (false == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
            System.out.println("返回错误结果：" + retCode);
            return null;
        } else {
            System.out.println("同步资费套餐结果成功：" + retCode);
        }

        //session.setAttribute("packages", responseObj.getBody());
        return null;

    }

    /**
     * 支付结果通知接口
     *
     * @return
     * @throws Exception
     */
//    @RequestMapping(value = "payNotify")
//    public void payNotify(PayNotifyRequestBody body,HttpServletResponse httpResponse) {
//        // String url = "http://wx.linksfield.net/TestService/test/payNotify";
//        String url = "http://localhost:80/TestService/test/payNotify";
//        // String url = Constant.BASE_URL;
//        System.out.println("=============" + body);
//        // 请求参数拼接
//        PayNotifyRequest request = new PayNotifyRequest();
//        RequestHeader header = getRequestHeader(Constant.cmd_payNotify);
//        request.setHeader(header);
//        request.setBody(body);
//        String sign = getSign(header, body, body.getClass());
//        request.setSign(sign);
//        // 将参数转换成XML格式
//        String sRequest = new JaxbUtil(PayNotifyRequest.class).toXml(request,
//                "UTF-8");
//        // 发送post请求，并接收返回结果
//        String result = HttpClientTool.sendHttpPost(url, sRequest);
//
//        PayNotifyResponse responseObj = new JaxbUtil(PayNotifyResponse.class)
//                .fromXml(result, "utf-8");
//        // 返回错误结果的处理
//        String retCode = responseObj.getHeader().getResultCode();
//        if (false == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
//            System.out.println("同步支付结果错误");
//        } else {
//            System.out.println("同步支付结果成功");
//    }
//
//        // 将支付结果存入本地数据库
//
//    }

    /**
     * 查询模块订购的套餐订单信息
     *
     * @return
     */
    @RequestMapping(value = "queryOrder")
    public String queryOrder(QueryOrderRequestBody body, Model model) {
        // String url = "http://wx.linksfield.net/TestService/test/queryOrder";
        String url = "http://localhost:80/TestService/test/queryOrder";
        // String url = Constant.BASE_URL;

        if (null == body.getDeviceId() || true == body.getDeviceId().isEmpty()) {
            model.addAttribute(Constant.ERROR_CODE, Constant.EC_PARAM);
            return Constant.ERROR_PAGE;
        } else {
            session.setAttribute("deviceId", body.getDeviceId());
            session.setAttribute("deviceType", body.getDeviceType());
        }

        // 请求参数拼接
        QueryOrderRequest request = new QueryOrderRequest();
        RequestHeader header = getRequestHeader(Constant.cmd_queryOrder);
        request.setHeader(header);
        request.setBody(body);
        String sign = getSign(header, body, body.getClass());
        request.setSign(sign);

        // 将参数转换成XML格式
        String sRequest = new JaxbUtil(QueryOrderRequest.class).toXml(request,
                "utf-8");

        // 发送post请求，并接收返回结果
        String result = HttpClientTool.sendHttpPost(url, sRequest);

        QueryOrderResponse responseObj = new JaxbUtil(QueryOrderResponse.class)
                .fromXml(result, "utf-8");

        // 返回错误结果的处理
        String retCode = responseObj.getHeader().getResultCode();
        if (false == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
            model.addAttribute(Constant.ERROR_CODE, retCode);
            return Constant.ERROR_PAGE;
        }

        session.setAttribute("orders", responseObj.getBody());
        session.setAttribute("deviceId", body.getDeviceId());
        return "orders";

    }

    /**
     * 订购包月流量套餐
     *
     * @return
     */
    @RequestMapping(value = "orderMonthlyPackage", method = RequestMethod.GET)
    public String orderMonthlyPackage(OrderMonthlyPackageRequestBody body, Model model) {

        String url = "http://localhost:80/TestService/test/orderMonthlyPackage";
        // String url = Constant.BASE_URL;

        String deviceId = (String) session.getAttribute("deviceId");
        body.setDeviceId(deviceId);

        String deviceType = (String) session.getAttribute("appScope");
        body.setDeviceType(deviceType);


        // 请求参数的处理
        if (null == body.getIsOpen()) {
            // 0表示不开通
            body.setIsOpen("0");
        } else {
            // 1表示开通
            body.setIsOpen("1");
        }

        // 请求参数拼接
        OrderMonthlyPackageRequest request = new OrderMonthlyPackageRequest();
        RequestHeader header = getRequestHeader(Constant.cmd_orderMonthlyPackage);
        request.setHeader(header);
        request.setBody(body);
        String sign = getSign(header, body, body.getClass());
        request.setSign(sign);

        // 将参数转换成XML格式
        String sRequest = new JaxbUtil(OrderMonthlyPackageRequest.class).toXml(
                request, "utf-8");

        // 发送post请求，并接收返回结果
        String result = HttpClientTool.sendHttpPost(url, sRequest);

        OrderMonthlyPackageResponse responseObj = new JaxbUtil(
                OrderMonthlyPackageResponse.class).fromXml(result, "utf-8");

        String retCode = responseObj.getHeader().getResultCode();
        if (false == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
            model.addAttribute(Constant.ERROR_CODE, retCode);
            return Constant.ERROR_PAGE;
        }

        // 在支付之前，原始信息：我们先把重要的信息先保存一份在深圳数据库里面
        // 北京回传的orderId
        String orderId = responseObj.getBody().getOrderId();
        String startData = responseObj.getBody().getOrderStartDate();
        String expireDate = responseObj.getBody().getOrderExpireDate();

        // 从请求中提取的信息

        deviceId = body.getDeviceId();
        deviceType = body.getDeviceType();

        String packageName = body.getPackageName();
        String packageCode = body.getPackageCode();

        String orderPeriod = body.getOrderPeriod();
        String payRst = body.getPayRst();
        String payType = body.getPayType();

        String payAmount = body.getPayAmount();
        String isOpen = body.getIsOpen();

        // 判定一下返回的orderId是不是为空
        if (null != orderId || orderId.length() > 0) {
            // 统一下单的原始数据类
//                     UnifiedOrder unifiedOrder = new UnifiedOrder();
//
//            unifiedOrder.setDeviceId(deviceId);
//            unifiedOrder.setDeviceType(deviceType);
//            unifiedOrder.setPackageName(packageName);
//            unifiedOrder.setPackageCode(packageCode);
//            unifiedOrder.setOrderPeriod(orderPeriod);
//            unifiedOrder.setPayRst(payRst);
//            unifiedOrder.setPayType(payType);
//            unifiedOrder.setPayAmount(payAmount);
//            unifiedOrder.setIsOpen(isOpen);
//            unifiedOrder.setOrderId(orderId);
//            unifiedOrder.setStartData(startData);
//            unifiedOrder.setExpireDate(expireDate);

            // internationalPayPackageService.unifiedOrder(unifiedOrder);
        }

        /* 测试时候使用这段代码 */
        // String amount = "1.00";
        // String amount = "36.00";
        // String refNo = responseObj.getBody().getOrderId();
        // // String currency = "MYR";
        // String currency = "HKD";
        // session.setAttribute("Amount", amount);
        // session.setAttribute("RefNo", refNo);
        // String sign1 = requestSignature(refNo, amount, currency);
        // session.setAttribute("signature", sign1);


        if ("0".equalsIgnoreCase(body.getPayType())) {

            // 表示ipay88支付

            return "ipay88/ipay";
        }
        if ("1".equalsIgnoreCase(body.getPayType())) {

            // 表示微信支付

            return "ipay88/ipay";
        }
        if ("2".equalsIgnoreCase(body.getPayType())) {

            // 表示paypal支付

            session.setAttribute("RefNo", responseObj.getBody().getOrderId());
            session.setAttribute("Amount", body.getPayAmount());
            return "paypal";
        }
        return null;
    }

    /**
     * 开通包月流量套餐
     *
     * @return
     */
    @RequestMapping(value = "openMonthlyPackage", method = RequestMethod.GET)
    public void openMonthlyPackage(OpenMonthlyPackageRequestBody body,
                                   HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
        String url = "http://localhost:80/TestService/test/openMonthlyPackage";
        // String url = Constant.BASE_URL ;

        body.setDeviceType((String) session.getAttribute("deviceType"));

        // 请求参数拼接
        OpenMonthlyPackageRequest request = new OpenMonthlyPackageRequest();
        RequestHeader header = getRequestHeader(Constant.cmd_openPackage);
        request.setHeader(header);
        request.setBody(body);
        String sign = getSign(header, body, body.getClass());
        request.setSign(sign);

        // 将参数转换成XML格式
        String sRequest = new JaxbUtil(OpenMonthlyPackageRequest.class).toXml(
                request, "utf-8");

        // 发送post请求，并接收返回结果
        String result = HttpClientTool.sendHttpPost(url, sRequest);

        OpenMonthlyPackageResponse responseObj = new JaxbUtil(
                OpenMonthlyPackageResponse.class).fromXml(result, "utf-8");
        if (responseObj.getHeader().getResultCode()
                .equalsIgnoreCase(Constant.RET_CODE_0000)) {
            requeryOrder();
            try {
                CommonResponse.response("OK", httpResponse);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            try {
                CommonResponse.response(
                        responseObj.getHeader().getResultInfo(), httpResponse);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    /**
     * 重新查询订单
     */
    private void requeryOrder() {
        QueryOrderRequestBody body = new QueryOrderRequestBody();
        body.setDeviceId((String) session.getAttribute("deviceId"));

        String url = "http://localhost:80/TestService/test/queryOrder";

        // 请求参数拼接
        QueryOrderRequest request = new QueryOrderRequest();
        RequestHeader header = getRequestHeader(Constant.cmd_queryOrder);
        request.setHeader(header);
        request.setBody(body);
        String sign = getSign(header, body, body.getClass());
        request.setSign(sign);

        // 将参数转换成XML格式
        String sRequest = new JaxbUtil(QueryOrderRequest.class).toXml(request,
                "utf-8");

        // 发送post请求，并接收返回结果
        String result = HttpClientTool.sendHttpPost(url, sRequest);

        // result="<?xml version='1.0' encoding='UTF-8'?><response><head><cmd_type>queryOrder</cmd_type><version>1.0</version><access_code>P000001</access_code><timestamp>1493893971</timestamp><ret_code>0000</ret_code><ret_info>success</ret_info></head><body><package_order_num>6</package_order_num><package_order><order_id>201801120001</order_id><package_name>香港包月流量套餐</package_name><package_code>00001</package_code><package_type>2</package_type><order_status>4</order_status><order_start_date>2018-01-15 12:09:31</order_start_date><order_planned_expire_date>2018-10-15 12:09:31</order_planned_expire_date><order_actual_expire_date>2</order_actual_expire_date></package_order><package_order><order_id>201801120001</order_id><package_name>北京包月流量套餐</package_name><package_code>00001</package_code><package_type>2</package_type><order_status>3</order_status><order_start_date>2018-01-15 12:09:31</order_start_date><order_planned_expire_date>2018-10-15 12:09:31</order_planned_expire_date><order_actual_expire_date>2</order_actual_expire_date></package_order><package_order><order_id>201801121111</order_id><package_name>上海包月流量套餐</package_name><package_code>00001</package_code><package_type>2</package_type><order_status>5</order_status><order_start_date>2018-01-15 12:09:31</order_start_date><order_planned_expire_date>2018-10-15 12:09:31</order_planned_expire_date><order_actual_expire_date>2</order_actual_expire_date></package_order><package_order><order_id>201801120001</order_id><package_name>武汉包月流量套餐</package_name><package_code>00001</package_code><package_type>2</package_type><order_status>5</order_status><order_start_date>2018-01-15 12:09:31</order_start_date><order_planned_expire_date>2018-10-15 12:09:31</order_planned_expire_date><order_actual_expire_date>2</order_actual_expire_date></package_order><package_order><order_id>201801120001</order_id><package_name>合肥包月流量套餐</package_name><package_code>00001</package_code><package_type>2</package_type><order_status>5</order_status><order_start_date>2018-01-15 12:09:31</order_start_date><order_planned_expire_date>2018-10-15 12:09:31</order_planned_expire_date><order_actual_expire_date>2</order_actual_expire_date></package_order><package_order><order_id>201801120001</order_id><package_name>杭州包月流量套餐</package_name><package_code>00001</package_code><package_type>2</package_type><order_status>5</order_status><order_start_date>2018-01-15 12:09:31</order_start_date><order_planned_expire_date>2018-10-15 12:09:31</order_planned_expire_date><order_actual_expire_date>2</order_actual_expire_date></package_order><page><cur_page_no>5</cur_page_no><total_pages>5</total_pages><total_count>81</total_count><page_size>20</page_size></page></body><sign>Kv06dozhVxcdGLMGD8u7LU8fz50=</sign></response>";
        QueryOrderResponse responseObj = new JaxbUtil(QueryOrderResponse.class)
                .fromXml(result, "utf-8");
        if (null != responseObj) {
            session.removeAttribute("orders");
            session.setAttribute("orders", responseObj.getBody());
        }

    }

    /**
     * 请求header信息拼装
     *
     * @param cmdType
     * @return
     */
    private RequestHeader getRequestHeader(String cmdType) {
        RequestHeader rHeader = new RequestHeader();
        rHeader.setVersion(Constant.VERSION);
        rHeader.setAccessCode("P000512");//  P000271
        rHeader.setCmdType(cmdType);
        //返回响应时的UTC时间戳（秒），和互联网时间误差不能超过 5 分钟。
        rHeader.setTimestamp(TimeUtil.getTimeInMillis().toString());
        return rHeader;
    }

    /**
     * 对head和body进行签名
     *
     * @param head
     * @param body
     * @return
     */
    private String getSign(RequestHeader head, Object body, Class cls) {

        // 对head进行xml编码，并去除xml头信息
        JaxbUtil jaxbUtil = new JaxbUtil(RequestHeader.class);
        String sHead = jaxbUtil.toXml(head, "utf-8");
        sHead = sHead.substring(sHead.indexOf("<head>"));

        // 对body进行xml编码，并去除xml头信息
        JaxbUtil jaxbUti2 = new JaxbUtil(cls);
        String sBody = jaxbUti2.toXml(body, "utf-8");
        sBody = sBody.substring(sBody.indexOf("<body>"));

        // 对head+body内容进行签名，并返回结果
        System.out.println("签名内容是：" + sHead + sBody);
        return SignUtil.generateSign(sHead + sBody, Constant.SECRET);

    }

}
